/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.store.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.thinkgem.jeesite.common.service.TreeService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.store.entity.StoreTree;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.store.dao.StoreTreeDao;

/**
 * 门店区域划分表Service
 * @author whh
 * @version 2017-09-06
 */
@Service
@Transactional(readOnly = true)
public class StoreTreeService extends TreeService<StoreTreeDao, StoreTree> {
	
	public static final String CACHE_STORETREE_LIST = "storeTreeList";
	
	private StoreTree entity = new StoreTree();
	
	public List<String> findChildsIdByStoreId(String storeId){
		return dao.findChildsIdByStoreId(storeId);
	}
	
	@SuppressWarnings("unchecked")
	public List<StoreTree> findByUser(boolean isCurrentSite, String module){
		
		List<StoreTree> list = (List<StoreTree>)UserUtils.getCache(CACHE_STORETREE_LIST);
		if (list == null){
			User user = UserUtils.getUser();
			StoreTree storeTree = new StoreTree();
			storeTree.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
			storeTree.setParent(new StoreTree());
			list = dao.findList(storeTree);
			// 将没有父节点的节点，找到父节点
			Set<String> parentIdSet = Sets.newHashSet();
			for (StoreTree e : list){
				if (e.getParent()!=null && StringUtils.isNotBlank(e.getParent().getId())){
					boolean isExistParent = false;
					for (StoreTree e2 : list){
						if (e.getParent().getId().equals(e2.getId())){
							isExistParent = true;
							break;
						}
					}
					if (!isExistParent){
						parentIdSet.add(e.getParent().getId());
					}
				}
			}
			if (parentIdSet.size() > 0){
				//FIXME 暂且注释，用于测试
//				dc = dao.createDetachedCriteria();
//				dc.add(Restrictions.in("id", parentIdSet));
//				dc.add(Restrictions.eq("delFlag", StoreTree.DEL_FLAG_NORMAL));
//				dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort"));
//				list.addAll(0, dao.find(dc));
			}
			UserUtils.putCache(CACHE_STORETREE_LIST, list);
		}
		
		/* (isCurrentSite){
			List<StoreTree> StoreTreeList = Lists.newArrayList(); 
			for (StoreTree e : list){
				if (StoreTree.isRoot(e.getId()) || (e.getSite()!=null && e.getSite().getId() !=null 
						&& e.getSite().getId().equals(Site.getCurrentSiteId()))){
					if (StringUtils.isNotEmpty(module)){
						if (module.equals(e.getModule()) || "".equals(e.getModule())){
							StoreTreeList.add(e);
						}
					}else{
						StoreTreeList.add(e);
					}
				}
			}
			return StoreTreeList;
		}*/
		return list;
	}

	public StoreTree get(String id) {
		return super.get(id);
	}
	
	public List<StoreTree> findList(StoreTree storeTree) {
		if (StringUtils.isNotBlank(storeTree.getParentIds())){
			storeTree.setParentIds(","+storeTree.getParentIds()+",");
		}
		return super.findList(storeTree);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreTree storeTree) {
		super.save(storeTree);
	}
	
	@Transactional(readOnly = false)
	public void delete(StoreTree storeTree) {
		super.delete(storeTree);
	}
	
}